package metier.outils;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

import javax.swing.ImageIcon;
import javax.swing.JButton;


import metier.modele.Client;
import metier.modele.Figure;

import client.Gestionnaire_Client;
import client.JCanvas;

/**
 * Outils pour le redimensionnement/déplacement des figures
 * @author Quentin, Vincent, Charlie
 *
 */
public class Tools_Edit extends JButton implements Tools
{

	private static final long serialVersionUID = -956535916480296341L;

	private Client client;
	private JCanvas jCanvas;
	private Figure figure;
	private Gestionnaire_Client gc;
	private int idFigure;
	private boolean inEdit = false;
	
	private int edit = RESIZE;
	public static int RESIZE = 0;
	public static int MOVE = 1;
	
	private MouseEvent premierClic;
	private int oldX = 0, oldY = 0, oldX2 = 0, oldY2 = 0;
	private int resizeCorner = 0; // 2 : haut gauche, 1: haut droite, 0: en dehors
	
	
	/**
	 * Constructeur
	 * @param client
	 * @param g
	 * @param panDessin
	 * @param nom
	 * @param imageIcon
	 */
	public Tools_Edit(Client client, Gestionnaire_Client gc,
		JCanvas panDessin, String nom, ImageIcon imageIcon)
	{
		super(imageIcon);
		setToolTipText(nom);
		addActionListener(gc);
		addKeyListener(gc);

		this.gc = gc;
		this.client = client;
		this.jCanvas = panDessin;
	}

	
	/**
	 * Début du clic
	 */
	public void debutClic(MouseEvent e)
	{
		inEdit = false;
		LinkedList<Figure> llf = jCanvas.getList();

		if (llf.size() > 0) 
		{
			int i;
			int in = 0;
			boolean on = false;
			for (i = llf.size()-1; i >= 0; i--) 
			{
				
				// RESIZE ?
				in = llf.get(i).pointInEditFigure(new Point(e.getX(), e.getY()));
				if (in != 0) 
				{
					edit = RESIZE;
					//setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
					break;
				}
				
				// MOVE ?
				on = llf.get(i).pointInFigure(new Point(e.getX(), e.getY()));
				if (on == true)
				{
					edit = MOVE;
					//setCursor(new Cursor(Cursor.HAND_CURSOR));
					break;
				}
			}
			if (i >= 0 && i < llf.size())
			{
				
				
				figure = llf.get(i);
				idFigure = i;
				inEdit = true;
				resizeCorner = in;
				
				oldX2 = figure.getX2();
				oldY2 = figure.getY2();
				oldX = figure.getX1();
				oldY = figure.getY1();
				
				premierClic = e;
								
			}
		}
	}

	/**
	 * Pendant le clic (drag)
	 */
	public void pendantClic(MouseEvent e)
	{
		if (inEdit == true)
		{
			if (edit == RESIZE)
			{
				int ex = e.getX();
				int ey = e.getY();
				int x = ex;
				int y = 0, x2 = 0, y2 = 0;
	            // Pour pas dépasser les bords
	            if (ex < 0)
	                    x = 0;
	            if (ey < 0)
	                    y = 0;
	            if (ex > jCanvas.getWidth())
	                    x = jCanvas.getWidth();
	            if (ey > jCanvas.getHeight())
	                    y = jCanvas.getHeight();
	            
	            if (figure instanceof Tools_Trait)
	            {
	            	if (resizeCorner == 2)
		            {
	            		x = ex;
	            		y = ey;
	            		x2 = oldX2;
	            		y2 = oldY2;
		            }
	            	else
	            	{
	            		x = oldX;
	            		y = oldY;
	            		x2 = ex;
	            		y2 = ey;
	            	}
		            if (gc.isDrawSquare() == true) // Touch 'shift' enfoncée ?
		            {
		            	
		            	if (resizeCorner == 2)
		            	{
		            		int dy = y - y2;
//		            		System.out.println("oldX,oldY: " + oldX + "," + oldY + " - dy: " + dy + " - (x2,y2): (" + x2 + "," + y2 + ") - (x,y): (" + x + "," + y + ")");
		            		int dx = x - x2;
							if (Math.abs(dx) > 2 * Math.abs(dy))
								y = y2;
							else if (Math.abs(dy) > 2* Math.abs(dx))
								x = x2;
							else
							{
								x = x2 + dx;
								if (dy > 0 && dx < 0)
									y = y2 - dx;
								else if (dy > 0 && dx > 0)
									y = y2 + dx;
								else if (dy < 0 && dx > 0)
									y = y2 - dx;
								else // dy < 0 && dx < 0
									y = y2 + dx;
							}
		            	}
		            	else
		            	{
		            		int dy = y2 - y;
//		            		System.out.println("oldX,oldY: " + oldX + "," + oldY + " - dy: " + dy + " - (x2,y2): (" + x2 + "," + y2 + ") - (x,y): (" + x + "," + y + ")");
		            		int dx = x2 - x;
							if (Math.abs(dx) > 2 * Math.abs(dy))
								y2 = y;
							else if (Math.abs(dy) > 2* Math.abs(dx))
								x2 = x;
							else
							{
								x2 = x + dx;
								if (dy > 0 && dx < 0)
									y2 = y - dx;
								else if (dy > 0 && dx > 0)
									y2 = y + dx;
								else if (dy < 0 && dx > 0)
									y2 = y - dx;
								else // dy < 0 && dx < 0
									y2 = y + dx;
							}
		            	}
		            }
	            }
	            else
	            {
		            if (resizeCorner == 2)
		            {
		            	// X, X2
		            	if (ex > oldX2)
		            	{
		            		x = oldX2;
		            		x2 = ex;
		            	}
		            	else
		            	{
		            		x = ex;
		            		x2 = oldX2;
		            	}
		            	// Y, Y2
		            	if (ey > oldY2)
		            	{
		            		y = oldY2;
		            		y2 = ey;
		            	}
		            	else
		            	{
		            		y = ey;
		            		y2 = oldY2;
		            	}
		            }	
		            else
		            {
		            	// X, X2
		            	if (ex < oldX)
		            	{
		            		x = oldX;
		            		x2 = ex;
		            	}
		            	else
		            	{
		            		x = ex;
		            		x2 = oldX;
		            	}
		            	
		            	// Y, Y2
		            	if (ey < oldY)
		            	{
		            		y = oldY;
		            		y2 = ey;
		            	}
		            	else
		            	{
		            		y = ey;
		            		y2 = oldY;
		            	}
		            }
		            
		            
		            if (gc.isDrawSquare() == true) // Touch 'shift' enfoncée ?
		            {
		            	if (resizeCorner == 2)
		            	{
		            		int dy = y2 - y;
		            		if (ex < oldX2)
		            			x = x2 - dy;
		            		else
		            			x2 = x + dy;
		            	}
		            	else
		            	{
		            		int dy = y - y2;
		            		int dx = x - x2;
		            		if (ex > oldX && ey > oldY)
		            			x = x2 + dy;
		            		if (ex > oldX && ey < oldY)
		            			x = x2 + dy;
		            		if (ex < oldX && ey > oldY)
		            			y = oldY + dx;
		            		if (ex < oldX && ey < oldY)
		            			x2 = x - dy;
		            		
//		            		System.out.println("oldX,oldY: " + oldX + "," + oldY + " - dy: " + dy + " - (x2,y2): (" + x2 + "," + y2 + ") - (x,y): (" + x + "," + y + ")");
		            	}
		            }
		            
	            }
	                    	
	            
	            figure.setDonnees(x, y, x2, y2);
			}
			else if (edit == MOVE)
			{
				int dx = premierClic.getX() - e.getX();
				int dy = premierClic.getY() - e.getY();
				int x = oldX - dx;
				int y = oldY - dy;
				int x2 = oldX2 - dx;
				int y2 = oldY2 - dy;
				
				figure.setDonnees(x, y, x2, y2);
			}
		
			jCanvas.repaint();
		}
	}

	
	/**
	 * Fin du clic
	 */
	public void finClic(MouseEvent e) 
	{
		
		if (inEdit == true)
		{
			setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			
			inEdit = false;
			
            if (client.isOnline())
            {           	
            	jCanvas.getList().get(idFigure).setStatus(Figure.EDIT);
				client.getCnx().Envoie(jCanvas.getList().get(idFigure));
            }
            else
            {
            	// Nothing to do
            }
			
			jCanvas.repaint();
		}
	}

}